package trading.algo.BullTask;

import java.text.DecimalFormat;
import java.util.TimerTask;

import trading.bo.EmailAccessorBO;
import trading.bo.IBAccessorBO;
import trading.dataentry.StockPositionOrOrder;

public class UpdateTrailAmountTask extends TimerTask {

    private IBAccessorBO ibAccessorBO;
    private EmailAccessorBO emailAccessorBO;
    private StockPositionOrOrder trailOrder;
    private double trailAmount;
    private double stopPrice;

    public UpdateTrailAmountTask(IBAccessorBO ibAccessorBO,
            EmailAccessorBO emailAccessorBO, StockPositionOrOrder trailOrder,
            double trailAmount, double stopPrice) {
        this.ibAccessorBO = ibAccessorBO;
        this.trailOrder = trailOrder;
        DecimalFormat df = new DecimalFormat("#.##");
        this.trailAmount = Double.parseDouble(df.format(trailAmount));
        this.emailAccessorBO = emailAccessorBO;
        this.stopPrice = Double.parseDouble(df.format(stopPrice));
    }

    @Override
    public void run() {
        this.ibAccessorBO.updateTrailStopOrder(
                Integer.parseInt(trailOrder.getOrderId()),
                trailOrder.getTicker(),
                Integer.parseInt(trailOrder.getQuantity()), trailAmount, stopPrice);
        this.emailAccessorBO.sendStockTrailOrderUpdateEmail(
                this.trailOrder.getOrderId(), this.trailOrder.getTicker(),
                this.trailOrder.getQuantity(),
                this.trailOrder.getTrailAmount(), trailAmount + "");
    }

}
